package club.beimeng.educenter.service.impl;

import club.beimeng.commonutils.JwtUtils;
import club.beimeng.commonutils.MD5;
import club.beimeng.educenter.entity.UcenterMember;
import club.beimeng.educenter.entity.vo.RegisterVo;
import club.beimeng.educenter.mapper.UcenterMemberMapper;
import club.beimeng.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author 小艾同学
 * @since 2021-08-02
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    // 登陆的方法
    @Override
    public String login(UcenterMember member) {
        // 获取登陆手机号和密码
        String mobile = member.getMobile();
        String password = member.getPassword();

        // 手机号和密码判空
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw new RuntimeException("登陆失败");
        }

        //判断手机号是否正确
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);
        UcenterMember mobileMember = baseMapper.selectOne(queryWrapper);

        //判断查询对象是否为空
        if (mobileMember == null){// 没有这个手机号
            throw new RuntimeException("没有手机号");
        }

        //判断密码
        if (!MD5.encrypt(password).equals(mobileMember.getPassword())){
            throw new RuntimeException("密码错误");
        }

        // 判断用户是否被禁用
        if (mobileMember.getIsDeleted()){
            throw new RuntimeException("账号被禁用");
        }

        //登陆成功
        // 生成一个token字符串
        return JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());
    }

    @Override
    public String loginEmail(UcenterMember member) {
        // 获取邮箱和密码
        String email = member.getEmail();
        String password = member.getPassword();

        // 手机号和密码判空
        if (StringUtils.isEmpty(email) || StringUtils.isEmpty(password)){
            throw new RuntimeException("登陆失败");
        }

        //判断邮箱号是否正确
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email",email);
        UcenterMember emailMember = baseMapper.selectOne(queryWrapper);

        //判断查询对象是否为空
        if (emailMember == null){// 没有找到
            throw new RuntimeException("邮箱未注册");
        }

        //判断密码
        if (!MD5.encrypt(password).equals(emailMember.getPassword())){
            throw new RuntimeException("密码错误");
        }

        // 判断用户是否被禁用
        if (emailMember.getIsDeleted()){
            throw new RuntimeException("账号被禁用");
        }

        //登陆成功
        // 生成一个token字符串
        return JwtUtils.getJwtToken(emailMember.getId(), emailMember.getNickname());
    }

    // 注册新用户
    @Override
    public Boolean register(RegisterVo registerVo) {
        // 获取注册的数据
        String emailCode = registerVo.getEmailCode();
        String mobileCode = registerVo.getMobileCode();
        String mobile = registerVo.getMobile();
        String email = registerVo.getEmail();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();

        //判断非空
        if (!(StringUtils.isEmpty(emailCode) || StringUtils.isEmpty(mobileCode))||!(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(email)) ||
                StringUtils.isEmpty(nickname) ||StringUtils.isEmpty(password)){
            throw new RuntimeException("注册失败-信息不全");
        }
        Boolean mobileCodeRight = false;
        Boolean emailCodeRight = false;
        if (!StringUtils.isEmpty(mobileCode)){
            mobileCodeRight = (mobileCode.equals(redisTemplate.opsForValue().get(mobile)));
        }
        if (!StringUtils.isEmpty(emailCode)){
            emailCodeRight = (emailCode.equals(redisTemplate.opsForValue().get(email)));
        }
        if (!(mobileCodeRight || emailCodeRight)){
            throw new RuntimeException("验证码错误");
        }else if (emailCodeRight && mobileCodeRight){
            throw new RuntimeException("只可用一种方式注册");
        }

        // 最终插入数据
        UcenterMember ucenterMember = new UcenterMember();

        // 判断手机号和email是否重复
        if (mobileCodeRight){
            QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("mobile",mobile);
            Integer count = baseMapper.selectCount(queryWrapper);
            if (count>0){
                throw new RuntimeException("手机号已被注册");
            }else {
                ucenterMember.setMobile(mobile);
            }
        }
        if (emailCodeRight){
            QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("email",email);
            Integer count = baseMapper.selectCount(queryWrapper);
            if (count>0){
                throw new RuntimeException("邮箱已被注册");
            }else {
                ucenterMember.setEmail(email);
            }
        }


        ucenterMember.setNickname(nickname);
        ucenterMember.setPassword(MD5.encrypt(password));
        ucenterMember.setIsDisabled(false);// 用户不禁用
        ucenterMember.setAvatar("https://img2.baidu.com/it/u=661539886,3900694712&fm=26&fmt=auto&gp=0.jpg");
        baseMapper.insert(ucenterMember);

        return null;
    }
}
